﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 撤销恢复操作
{
    public class Grahpic
    {
        public Grahpic(string zoom)
        {
            Zoom = zoom;
        }
        public override string ToString()
        {
            return Zoom.ToString();
        }
        public string Zoom { get; set; }
    }

    public class Work<T> where T : Grahpic
    {
        public int WorkLenght { get; set; } = 20;
        private int currentIndex = 0;
        public int CurrentIndex => currentIndex;
        public List<int> WorkList = new List<int>();
        private List<Grahpic> graphics = new List<Grahpic>();
        public Work()
        {

        }
        public bool DoSomething(Grahpic graphic)
        {
            if (currentIndex == graphics.Count())
            {
                if(graphics.Count()>WorkLenght)
                currentIndex++;
                graphics.Add(graphic);
                graphics.RemoveAt(0);
            }
            else
            {

                graphics.RemoveRange(currentIndex , graphics.Count() - currentIndex);
                currentIndex++;
                graphics.Add(graphic);

            }
            Console.WriteLine(this.ToString());

            return true;
        }
        public override string ToString()
        {
            if (graphics.Count() == 0)
            {
                return "无操作记录";
            }
            string operationstring = "";
            operationstring += $@"共{graphics.Count()}步,当前在第{currentIndex}步;以下是操作详情{Environment.NewLine}";
            for (var i = 0; i < graphics.Count(); i++)
            {
                operationstring += $"第{i + 1}步{graphics[i]}";
                if (i == currentIndex-1) { operationstring += "（当前）"; };
            }
            operationstring += Environment.NewLine;
            return operationstring;
        }


        public bool CanRedo => currentIndex > 0;
        public bool CanUnDo => currentIndex < graphics.Count();
        public int ReDo()
        {
            if (CanRedo)
            {
                currentIndex--;
            }
            Console.WriteLine(this.ToString());
            return currentIndex;
        }
        public int UnDo()
        {
            if (CanUnDo)
            {

                currentIndex++;
            }
            Console.WriteLine(this.ToString());
            return currentIndex;
        }
    }
}
